#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Autor: Pedro Moreira, 10015
# Data: 08/12/2011


import wx
import csv2SQL
from Teachers import Teachers

class RebidesStats(wx.Frame):
    '''
    GUI para gerar estatística sobre os dados Rebides 
    '''
    def __init__(self, *args, **kwds):
        # begin wxGlade: RebidesStats.__init__
        self.teachers = Teachers() # inicializar teachers com os dados
        ###################################
        ## CRIAR COMPONENTES
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.labelEstabelecimento = wx.StaticText(self, -1, "Nome de Estabelecimento:")
        self.comboEstabelecimento = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.labelCategoria = wx.StaticText(self,
                                            3-1, "Categoria:")
        self.comboCategoria = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.labelGrau = wx.StaticText(self, -1, "Grau:")
        self.comboGrau = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.labelUrl = wx.StaticText(self, -1, "URL p/contar tags:")
        self.textUrl = wx.TextCtrl(self, -1, "")
        self.buttonHTML = wx.Button(self, -1, "criar HTML")
        self.buttonCSV = wx.Button(self, -1, "criar CSV")
        self.buttonGraph = wx.Button(self, -1, u"criar gráfico")
        self.buttonTags = wx.Button(self, -1, "contar tags/url")
        self.convertSQL = wx.Button(self, -1, "Converter CSV2SQL")
        ###################################

        
        ###################################
        ## CONFIGURAR COMPONENTES
        ## E DESENHAR INTERFACE
        self.__set_properties()
        self.__do_layout()
        ###################################

        # END __init__


    def __set_properties(self):
        '''
        configurar componentes
        '''
        ###################################
        ## CONFIGURAÇÕES
        self.SetTitle("Rebides Statistics")
        self.SetSize((500, 210))
        self.comboEstabelecimento.SetMinSize((490, -1))
        self.comboCategoria.SetMinSize((490, -1))
        self.comboGrau.SetMinSize((490, -1))
        self.textUrl.SetMinSize((490, -1))
        self.buildCombos() # adicionar conteúdos às comboBoxes
        ###################################
        
        ###################################
        ## ACÇÕES PARA OS BOTÕES
        self.Bind(wx.EVT_BUTTON, self.evt_genHTML, self.buttonHTML)
        self.Bind(wx.EVT_BUTTON, self.evt_genCSV, self.buttonCSV)
        self.Bind(wx.EVT_BUTTON, self.evt_genGraph, self.buttonGraph)
        self.Bind(wx.EVT_BUTTON, self.evt_countTags, self.buttonTags)
        self.Bind(wx.EVT_BUTTON, self.evt_csv2SQL, self.convertSQL)
        ###################################

        # END __set_properties


    def __do_layout(self):
        '''
        desenhar interface
        '''
        # begin wxGlade: RebidesStats.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.labelEstabelecimento, 0, 0, 0)
        sizer_2.Add(self.comboEstabelecimento, 0, 0, 0)
        sizer_2.Add(self.labelCategoria, 0, 0, 0)
        sizer_2.Add(self.comboCategoria, 0, 0, 0)
        sizer_2.Add(self.labelGrau, 0, 0, 0)
        sizer_2.Add(self.comboGrau, 0, 0, 0)
        sizer_2.Add(self.labelUrl, 0, 0, 0)
        sizer_2.Add(self.textUrl, 0, 0, 0)
        sizer_3.Add(self.convertSQL, 0, 0, 0)
        sizer_3.Add(self.buttonHTML, 0, 0, 0)
        sizer_3.Add(self.buttonCSV, 0, 0, 0)
        sizer_3.Add(self.buttonGraph, 0, 0, 0)
        sizer_3.Add(self.buttonTags, 0, 0, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()

        # END __do_layout


    def buildCombos(self):
        '''
        Preencher comboBoxes com os conteúdos
        '''
        estabelecimentos = self.teachers.getField(8)
        categorias = self.teachers.getField(4)
        graus = self.teachers.getField(2)
        self.comboEstabelecimento.Append("");
        self.comboCategoria.Append("");
        self.comboGrau.Append("");
        for est in estabelecimentos:
            self.comboEstabelecimento.Append(est)
            pass
        for cat in categorias:
            self.comboCategoria.Append(cat)
            pass
        for gr in graus:
            self.comboGrau.Append(gr)
            pass
        pass

        # END buildCombos
    
    
    def evt_genHTML(self, event): # wxGlade: RebidesStats.<event_handler>
        '''
        criar html a partir dos dados seleccionados
        '''
        self.teachers.genHTML(self.comboEstabelecimento.GetCurrentSelection(),
                              self.comboCategoria.GetCurrentSelection(),
                              self.comboGrau.GetCurrentSelection()
                              )
        pass

        # END evt_genHTML

    def evt_csv2SQL(self, event):
        csv2SQL.autoInsertion()
        
    def evt_genCSV(self, event): # wxGlade: RebidesStats.<event_handler>
        '''
        criar ficheiro CSV a partir dos dados seleccionados
        '''
        self.teachers.genCSV(self.comboEstabelecimento.GetCurrentSelection(),
                              self.comboCategoria.GetCurrentSelection(),
                              self.comboGrau.GetCurrentSelection()
                              )
        pass
        # END evt_genCSV
        

    def evt_genGraph(self, event): # wxGlade: RebidesStats.<event_handler>
        '''
        criar gráfico a partir dos dados seleccionados
        '''
        self.teachers.genGraph(self.comboEstabelecimento.GetCurrentSelection(),
                               self.comboCategoria.GetCurrentSelection(),
                               self.comboGrau.GetCurrentSelection()
                               )
        pass
        # END genGraph
        

    def evt_countTags(self, event): # wxGlade: RebidesStats.<event_handler>
        '''
        efectuar contagem de tags no url fornecido
        '''
        self.teachers.countTags(self.textUrl.GetValue())
        pass
        # END evt_countTags
    

    # END OF CLASS RebidesStats


if __name__ == "__main__":
    RebidesStatistics = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    RSFRM = RebidesStats(None, -1, "")
    RebidesStatistics.SetTopWindow(RSFRM)
    RSFRM.Show()
    RebidesStatistics.MainLoop()
